<?php

class SupplierLocationAction extends CommonAction{
public function index()
{
$reminder = M('RemindCount')->find();
$reminder['dp_count_time'] = get_gmtime();
M('RemindCount')->save($reminder);
$condition = ' deal_type= 0 ';
if(intval($_REQUEST['supplier_id'])>0)
{
es_session::set('admin_supplier_id',intval($_REQUEST['supplier_id']));
$this->assign('supplier_info',$supplier_info);
$condition = ' supplier_id = '.intval($_REQUEST['supplier_id']);;
}
$page_idx = intval($_REQUEST['p'])==0?1:intval($_REQUEST['p']);
$page_size = C('PAGE_LISTROWS');
$limit = (($page_idx-1)*$page_size).','.$page_size;
if (isset ( $_REQUEST ['_order'] )) {
$order = $_REQUEST ['_order'];
}
if (isset ( $_REQUEST ['_sort'] )) {
$sort = $_REQUEST ['_sort'] ?'asc': 'desc';
}else {
$sort = $asc ?'asc': 'desc';
}
$total = $GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX."supplier_location where $condition");
if($total<50000)
{
if($order=='')
$order = 'id';
$orderby = 'order by '.$order.' '.$sort;
}
else
{
if($order!='is_effect'&&$order!='is_recommend'&&$order!='is_verify'&&$order!='is_main'&&$order!='new_dp_count')
{
$orderby = '';
}
else
{
$orderby = 'order by '.$order.' '.$sort;
}
}
if(trim($_REQUEST['name'])!='')
{
if($total<50000)
{
$list = $GLOBALS['db']->getAll('select * from '.DB_PREFIX."supplier_location where name like '%".trim($_REQUEST['name'])."%' and $condition  $orderby limit ".$limit);
$total = $GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX."supplier_location where name like '%".trim($_REQUEST['name'])."%' and $condition ");
}
else
{
$kws_div = div_str(trim($_REQUEST['name']));
foreach($kws_div as $k=>$item)
{
$kw[$k] = str_to_unicode_string($item);
}
$kw_unicode = implode(' ',$kw);
$list = $GLOBALS['db']->getAll('select * from '.DB_PREFIX."supplier_location where match(`name_match`) against('".$kw_unicode."' IN BOOLEAN MODE) and $condition  $orderby limit ".$limit);
$total = $GLOBALS['db']->getOne('select * from '.DB_PREFIX."supplier_location where match(`name_match`) against('".$kw_unicode."' IN BOOLEAN MODE) and $condition");
}
}
else
{
$list= $GLOBALS['db']->getAll('select * from '.DB_PREFIX."supplier_location where $condition $orderby limit ".$limit);
$total = $GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX."supplier_location where $condition ");
}
$p = new Page ( $total,'');
$page = $p->show ();
$sortImg = $sort;
$sortAlt = $sort == 'desc'?l('ASC_SORT') : l('DESC_SORT');
$sort = $sort == 'desc'?1 : 0;
$this->assign ( 'sort',$sort );
$this->assign ( 'order',$order );
$this->assign ( 'sortImg',$sortImg );
$this->assign ( 'sortType',$sortAlt );
$this->assign ( 'list',$list );
$this->assign ( 'page',$page );
$this->assign ( 'nowPage',$p->nowPage);
$this->display ();
return;
}
public function add()
{
$supplier_id = intval(es_session::get('admin_supplier_id'));
$supplier_info = M('Supplier')->getById($supplier_id);
$this->assign('supplier_info',$supplier_info);
$city_list = M('DealCity')->where('is_delete = 0')->findAll();
$city_list = D('DealCity')->toFormatTree($city_list,'name');
$this->assign('city_list',$city_list);
$brand_list = M('Brand')->findAll();
$this->assign('brand_list',$brand_list);
$this->display();
}
public function area_list()
{
$id =  intval($_REQUEST['id']);
$area_list = M('Area')->where('city_id='.intval($_REQUEST['city_id']))->findAll();
foreach($area_list as $k=>$v)
{
if(M('SupplierLocationAreaLink')->where('area_id='.$v['id'].' and location_id = '.$id)->count())
{
$area_list[$k]['checked'] = true;
}
}
$this->assign('area_list',$area_list);
$this->display();
}
public function search_supplier()
{
if(intval($GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'supplier'))<50000)
$sql  ='select * from '.DB_PREFIX."supplier where name like '%".trim($_REQUEST['key'])."%' limit 30";
else 
{
$kws_div = div_str(trim($_REQUEST['key']));
foreach($kws_div as $k=>$item)
{
$kw[$k] = str_to_unicode_string($item);
}
$kw_unicode = implode(' ',$kw);
$sql = 'select * from '.DB_PREFIX."supplier where (match(name_match) against('".$kw_unicode."' IN BOOLEAN MODE)) limit 30";
}
$supplier_list = $GLOBALS['db']->getAll($sql);
$this->assign('supplier_list',$supplier_list);
$this->display();
}
public function insert() {
B('FilterString');
$ajax = intval($_REQUEST['ajax']);
$data = M(MODULE_NAME)->create ();
$this->assign('jumpUrl',u(MODULE_NAME.'/add'));
if(!check_empty($data['name']))
{
$this->error(L('LOCATION_NAME_EMPTY_TIP'));
}
if($data['supplier_id'] == 0)
{
$supplier_data['name'] = $data['name'];
$supplier_data['is_effect'] = 1;
$supplier_id = M('Supplier')->add($supplier_data);
syn_supplier_match($supplier_id);
$data['supplier_id'] = $supplier_id;
}
$data['online_info'] = htmlspecialchars($data['online_info']);
$data['is_effect'] = 1;
$data['is_recommend'] = intval($_REQUEST['is_recommend']);
$data['is_bank'] = intval($_REQUEST['is_bank']);
$data['no_smoking'] = intval($_REQUEST['no_smoking']);
$data['takeaway'] = intval($_REQUEST['takeaway']);
$data['one_line_order'] = intval($_REQUEST['one_line_order']);
$data['takeaway_time'] = trim($data['takeaway_time'])==''?0:to_timespan($data['takeaway_time']);
$log_info = $data['name'];
$list=M(MODULE_NAME)->add($data);
if (false !== $list) {
if($data['is_main']==1)
{
M(MODULE_NAME)->where('supplier_id='.$data['supplier_id'].' and id <> '.$list)->setField('is_main',0);
}
if(M(MODULE_NAME)->where('supplier_id='.$data['supplier_id'].' and is_main = 1')->count()==0)
{
M(MODULE_NAME)->where('id='.$list)->setField('is_main',1);
}
$area_ids = $_REQUEST['area_id'];
foreach($area_ids as $area_id)
{
$area_data['area_id'] = $area_id;
$area_data['location_id'] = $list;
M('SupplierLocationAreaLink')->add($area_data);
}
$purpose_ids = $_REQUEST['purpose_id'];
foreach($purpose_ids as $purpose_id)
{
$purpose_data['purpose_id'] = $purpose_id;
$purpose_data['location_id'] = $list;
M('SupplierLocationPurposeLink')->add($purpose_data);
}
$purpose_type_ids = $_REQUEST['purpose_type_id'];
foreach($purpose_type_ids as $purpose_type_id)
{
$purpose_type_data['purpose_type_id'] = $purpose_type_id;
$purpose_type_data['location_id'] = $list;
M('SupplierLocationPurposeTypeLink')->add($purpose_type_data);
}
$brand_ids = $_REQUEST['brand_id'];
foreach($brand_ids as $brand_id)
{
$brand_data['brand_id'] = $brand_id;
$brand_data['location_id'] = $list;
M('SupplierLocationBrandLink')->add($brand_data);
}
foreach($_REQUEST['deal_cate_type_id'] as $type_id)
{
$link_data = array();
$link_data['deal_cate_type_id'] = $type_id;
$link_data['location_id'] = $list;
M('DealCateTypeLocationLink')->add($link_data);
}
$group_ids = $_REQUEST['group_id'];
foreach($group_ids as $gid=>$preset)
{
if(trim($preset)!='')
{
$link['group_id'] = $gid;
$link['supplier_location_id'] = $list;
$link['preset'] = trim($preset);
M('SupplierTagGroupPreset')->add($link);
}
}
$show_group_ids = $_REQUEST['show_group_id'];
$exist_tags_array = array();
foreach($show_group_ids as $gid=>$row)
{
if(trim($row)!='')
{
$show_tags_array = preg_split('[ |,]',$row);
foreach($show_tags_array as $kk=>$rr)
{
$rs = explode('|',$rr);
$tag = trim($rs[0]);
$count = intval($rs[1]);
$exist_tags_array[] = "'".$tag."'";
if(trim($tag)!='')
{
$tag_rs = array();
$tag_rs['tag_name'] = $tag;
$tag_rs['supplier_location_id'] = $list;
$tag_rs['group_id'] = $gid;
$tag_rs['total_count'] = $count;
M('SupplierTag')->add($tag_rs);
}
}
}
}
clear_auto_cache('store_filter_nav_cache');
syn_supplier_location_match($list);
save_log($log_info.L('INSERT_SUCCESS'),1);
$this->success(L('INSERT_SUCCESS'));
}else {
save_log($log_info.L('INSERT_FAILED'),0);
$this->error(L('INSERT_FAILED'));
}
}
public function edit() {
$id = intval($_REQUEST ['id']);
$condition['id'] = $id;
$vo = M(MODULE_NAME)->where($condition)->find();
$supplier_info = M('Supplier')->getById($vo['supplier_id']);
if(!$supplier_info)
{
$this->error(l('SUPPLIER_NOT_EXIST'));
}
$this->assign('supplier_info',$supplier_info);
$city_list = M('DealCity')->where('is_delete = 0')->findAll();
$city_list = D('DealCity')->toFormatTree($city_list,'name');
$this->assign('city_list',$city_list);
$brand_list = M('Brand')->findAll();
foreach($brand_list as $k=>$v)
{
if(M('SupplierLocationBrandLink')->where('location_id='.$vo['id'].' and brand_id='.$v['id'])->count()>0)
{
$brand_list[$k]['checked'] = true;
}
}
$this->assign('brand_list',$brand_list);
$this->assign ( 'vo',$vo );
$this->display ();
}
public function update() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$log_info = M(MODULE_NAME)->where('id='.intval($data['id']))->getField('name');
$this->assign('jumpUrl',u(MODULE_NAME.'/edit',array('id'=>$data['id'])));
if(!check_empty($data['name']))
{
$this->error(L('LOCATION_NAME_EMPTY_TIP'));
}
if($data['supplier_id']==0)
{
$this->error(L('SUPPLIER_NOT_EXIST'));
}
$data['online_info'] = htmlspecialchars($data['online_info']);
$data['is_recommend'] = intval($_REQUEST['is_recommend']);
$data['is_bank'] = intval($_REQUEST['is_bank']);
$data['no_smoking'] = intval($_REQUEST['no_smoking']);
$data['takeaway'] = intval($_REQUEST['takeaway']);
$data['one_line_order'] = intval($_REQUEST['one_line_order']);
$data['takeaway_time'] = trim($data['takeaway_time'])==''?0:to_timespan($data['takeaway_time']);
$list=M(MODULE_NAME)->save ($data);
if (false !== $list) {
M('SupplierLocationAreaLink')->where('location_id='.$data['id'])->delete();
$area_ids = $_REQUEST['area_id'];
foreach($area_ids as $area_id)
{
$area_data['area_id'] = $area_id;
$area_data['location_id'] = $data['id'];
M('SupplierLocationAreaLink')->add($area_data);
}
M('SupplierLocationPurposeLink')->where('location_id='.$data['id'])->delete();
foreach($_REQUEST['purpose_id'] as $purpose_id)
{
$purpose_data['purpose_id'] = $purpose_id;
$purpose_data['location_id'] = $data['id'];
M('SupplierLocationPurposeLink')->add($purpose_data);
}
M('SupplierLocationPurposeTypeLink')->where('location_id='.$data['id'])->delete();
foreach($_REQUEST['purpose_type_id'] as $purpose_type_id)
{
$purpose_type_data['purpose_type_id'] = $purpose_type_id;
$purpose_type_data['location_id'] = $data['id'];
M('SupplierLocationPurposeTypeLink')->add($purpose_type_data);
}
M('SupplierLocationBrandLink')->where('location_id='.$data['id'])->delete();
$brand_ids = $_REQUEST['brand_id'];
foreach($brand_ids as $brand_id)
{
$brand_data['brand_id'] = $brand_id;
$brand_data['location_id'] = $data['id'];
M('SupplierLocationBrandLink')->add($brand_data);
}
M('DealCateTypeLocationLink')->where('location_id='.$data['id'])->delete();
foreach($_REQUEST['deal_cate_type_id'] as $type_id)
{
$link_data = array();
$link_data['deal_cate_type_id'] = $type_id;
$link_data['location_id'] = $data['id'];
M('DealCateTypeLocationLink')->add($link_data);
}
M('SupplierTagGroupPreset')->where('supplier_location_id='.$data['id'])->delete();
$group_ids = $_REQUEST['group_id'];
foreach($group_ids as $gid=>$preset)
{
if(trim($preset)!='')
{
$link['group_id'] = $gid;
$link['supplier_location_id'] = $data['id'];
$link['preset'] = trim($preset);
M('SupplierTagGroupPreset')->add($link);
}
}
$show_group_ids = $_REQUEST['show_group_id'];
$exist_tags_array = array("''");
foreach($show_group_ids as $gid=>$row)
{
if(trim($row)!='')
{
$show_tags_array = preg_split('[ |,]',$row);
foreach($show_tags_array as $kk=>$rr)
{
$rs = explode('|',$rr);
$tag = trim($rs[0]);
$count = intval($rs[1]);
$exist_tags_array[] = "'".$tag."'";
$tag_rs = M('SupplierTag')->where("tag_name = '".$tag."' and supplier_location_id = ".$data['id'].' and group_id = '.$gid)->find();
if($tag_rs)
{
$tag_rs['total_count'] = $count;
$GLOBALS['db']->query('update '.DB_PREFIX.'supplier_tag set total_count = '.$count." where tag_name = '".$tag."' and supplier_location_id = ".$data['id'].' and group_id = '.$gid);
}
else
{
if(trim($tag)!='')
{
$tag_rs = array();
$tag_rs['tag_name'] = $tag;
$tag_rs['supplier_location_id'] = $data['id'];
$tag_rs['group_id'] = $gid;
$tag_rs['total_count'] = $count;
M('SupplierTag')->add($tag_rs);
}
}
}
}
}
$GLOBALS['db']->query('delete from '.DB_PREFIX.'supplier_tag where tag_name not in ('.implode(',',$exist_tags_array).') and supplier_location_id = '.$data['id']);
syn_supplier_location_match($data['id']);
save_log($log_info.L('UPDATE_SUCCESS'),1);
$this->success(L('UPDATE_SUCCESS'));
}else {
save_log($log_info.L('UPDATE_FAILED'),0);
$this->error(L('UPDATE_FAILED'),0,$log_info.L('UPDATE_FAILED'));
}
}
public function set_sort()
{
$id = intval($_REQUEST['id']);
$sort = intval($_REQUEST['sort']);
$log_info = M(MODULE_NAME)->where('id='.$id)->getField('name');
if(!check_sort($sort))
{
$this->error(l('SORT_FAILED'),1);
}
M(MODULE_NAME)->where('id='.$id)->setField('sort',$sort);
save_log($log_info.l('SORT_SUCCESS'),1);
$this->success(l('SORT_SUCCESS'),1);
}
public function foreverdelete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
if(M('SupplierLocationDp')->where(array ('supplier_location_id'=>array ('in',explode ( ',',$id ) ) ))->count()>0)
{
$this->error ('请先清空商户的点评',$ajax);
}
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->delete();
if ($list!==false) {
clear_auto_cache('store_filter_nav_cache');
M('SupplierTagGroupPreset')->where(array ('supplier_location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierTag')->where(array ('supplier_location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierLocationPointResult')->where(array ('supplier_location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('DealLocationLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('YouhuiLocationLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('DealCateTypeLocationLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierAccountLocationLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('TagUserVote')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierLocationImages')->where(array ('supplier_location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierLocationAreaLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierLocationPurposeLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierLocationPurposeTypeLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
M('SupplierLocationBrandLink')->where(array ('location_id'=>array ('in',explode ( ',',$id ) ) ))->delete();
save_log($info.l('FOREVER_DELETE_SUCCESS'),1);
$this->success (l('FOREVER_DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('FOREVER_DELETE_FAILED'),0);
$this->error (l('FOREVER_DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
function load_cate(){
$city_id = intval($_REQUEST['city_id']);
$location_id = intval($_REQUEST['location_id']);
$deal_cate_tree = $GLOBALS['db']->getAll('SELECT dc.* FROM '.DB_PREFIX.'deal_cate dc LEFT JOIN '.DB_PREFIX."deal_cate_city_link dccl ON dccl.cate_id=dc.id where dccl.city_id=$city_id AND dc.is_delete = 0 ORDER BY dc.id ASC");
$deal_cate_tree = D('DealCate')->toFormatTree($deal_cate_tree,'name');
if($location_id >0){
foreach($deal_cate_tree as $k=>$v){
$deal_cate_tree[$k]['checked'] = $GLOBALS['db']->getOne('SELECT count(*) FROM '.DB_PREFIX.'supplier_location WHERE id='.$location_id.' AND deal_cate_id='.$v['id']);
}
}
$this->assign('deal_cate_tree',$deal_cate_tree);
if($deal_cate_tree)
$result['status'] = 1;
else
$result['status'] = 0;
$result['html'] = $this->fetch();
$this->ajaxReturn($result['html'],'',$result['status']);
}
function load_sub_cate()
{
$cate_id = intval($_REQUEST['cate_id']);
$city_id = intval($_REQUEST['city_id']);
$location_id = intval($_REQUEST['location_id']);
$sub_cate_list = $GLOBALS['db']->getAll('select c.* from '.DB_PREFIX.'deal_cate_type as c left join '.DB_PREFIX.'deal_cate_type_link as l on l.deal_cate_type_id = c.id LEFT JOIN '.DB_PREFIX."deal_cate_type_city_link dctcl ON dctcl.cate_type_id=c.id AND dctcl.cate_id=l.cate_id where dctcl.city_id=$city_id AND l.cate_id = ".$cate_id);
foreach($sub_cate_list as $k=>$v)
{
$sub_cate_list[$k]['checked'] = $GLOBALS['db']->getOne('select count(*) from '.DB_PREFIX.'deal_cate_type_location_link where deal_cate_type_id = '.$v['id'].' and location_id = '.$location_id);
}
$this->assign('sub_cate_list',$sub_cate_list);
if($sub_cate_list)
$result['status'] = 1;
else
$result['status'] = 0;
$result['html'] = $this->fetch();
$this->ajaxReturn($result['html'],'',$result['status']);
}
function load_tag_list()
{
$cate_id = intval($_REQUEST['cate_id']);
$id= intval($_REQUEST['id']);
$group_list = M()->query('select g.* from '.DB_PREFIX.'tag_group as g left join '.DB_PREFIX.'tag_group_link as gl on g.id = gl.tag_group_id where gl.category_id = '.$cate_id);
foreach($group_list as $k=>$v)
{
$sql = 'select * from '.DB_PREFIX.'supplier_tag where supplier_location_id = '.$id.' and group_id = '.$v['id'];
$res = $GLOBALS['db']->getAll($sql);
$tags=array();
foreach($res as $kk=>$vv)
{
$tags[] = $vv['tag_name'].'|'.$vv['total_count'];
}
$group_list[$k]['show_value'] = implode(' ',$tags);
$group_list[$k]['value'] = $GLOBALS['db']->getOne('select `preset` from '.DB_PREFIX.'supplier_tag_group_preset where group_id = '.$v['id'].' and supplier_location_id = '.$id);
}
$this->assign('group_list',$group_list);
$this->display();
}
public function load_purpose_list(){
$city_id = intval($_REQUEST['city_id']);
$id= intval($_REQUEST['id']);
$purpose = 	$GLOBALS['db']->getAll('SELECT pc.* FROM '.DB_PREFIX.'purpose_cate pc LEFT JOIN '.DB_PREFIX.'purpose_cate_city_link pccl ON pccl.cate_id=pc.id WHERE pccl.city_id='.$city_id.' group by pc.id ORDER BY pc.sort DESC, pc.id DESC');
foreach($purpose as $k=>$v)
{
$purpose[$k]['checked'] = $GLOBALS['db']->getOne('SELECT count(*) FROM '.DB_PREFIX.'supplier_location_purpose_link WHERE location_id='.$id.' AND purpose_id='.$v['id']);
$purpose[$k]['sub'] = $GLOBALS['db']->getAll('SELECT pct.* FROM '.DB_PREFIX.'purpose_cate_type pct LEFT JOIN '.DB_PREFIX.'purpose_cate_type_city_link pctcl ON pct.id=pctcl.cate_type_id WHERE pct.cate_id='.$v['id'].' AND pctcl.city_id='.$city_id.' group by pct.id  ORDER BY pct.sort DESC,pct.id DESC');
foreach($purpose[$k]['sub'] as $kk=>$vv){
$purpose[$k]['sub'][$kk]['checked'] = $GLOBALS['db']->getOne('SELECT count(*) FROM '.DB_PREFIX.'supplier_location_purpose_type_link WHERE location_id='.$id.' AND purpose_type_id='.$vv['id']);
}
}
$this->assign('purpose',$purpose);
$this->display('purpose_list');
}
}

?>